package com.yksj.healthtalk.ui.doctorstation.active;


import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.ui.R;

/**
 * 
 * 活动介绍
 * @author origin
 *l
 */
public class ActivesProduceDescActivity extends BaseFragmentActivity implements OnClickListener {
	
	TextView contentTextV;
	JSONObject mJson;;
	Button button;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.actives_produce_desc_layout);
		
		initUI();
		
		initData();
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		
		RequestParams params = new RequestParams();
		params.put("customerId", getIntent().getStringExtra("customerId"));
		params.put("activityId",getIntent().getStringExtra("activityId"));
		HttpRestClient.doHttpFindIsGetPrize(params,
				new JsonsfHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				mJson = response;
				updateUI();
			}
		});
	}
			
			
	
	private void initUI(){
		initTitle();
		setBackBtn("返回",this);
		contentTextV = (TextView)findViewById(R.id.textView1);
		contentTextV.setMovementMethod(ScrollingMovementMethod.getInstance());
		setTitle(getIntent().getStringExtra("title"));
		button  = (Button)findViewById(R.id.activity_join);
		String json = getIntent().getStringExtra("json");
		mJson  = JSON.parseObject(json);
		updateUI();
	}
	
	private void updateUI(){
		//不能抽奖
		if(mJson.containsKey("error_message")){
			button.setText("马上参与");
			//继续抽奖
			button.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),mJson.getString("error_message"));
				}
			});
			return;
		}
		int isGet = mJson.getIntValue("isGet");
		//抽奖
		if(isGet == 0){
			button.setText("马上参与");
			//继续抽奖
			button.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = getIntent();
					intent.putExtra("title", mJson.getString("ACTIVITIES_NAME"));
					intent.putExtra("json", mJson.getJSONArray("data").toJSONString());
					intent.setClass(ActivesProduceDescActivity.this, LucklyDrawActivity.class);
					startActivityForResult(intent, 1);
				}
			});
		//查看奖品	
		}else{
			if(mJson.getJSONArray("data").getJSONObject(0).getString("RECEIVING_FLAG").equals("0")){
				button.setText("马上参与");
				button.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
						SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),"您已经参与过此次活动，不能重复参与。请您随时关注新的活动。");
					}
				});
			}else{
				button.setText("查看奖品");
				//继续抽奖
				button.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = getIntent();
						intent.putExtra("json",  mJson.getJSONArray("data").getJSONObject(0).toJSONString());
						intent.setClass(ActivesProduceDescActivity.this, PrizeDetailActivity.class);
						startActivityForResult(intent, 2);
					}
				});
			}
		}
	}
	
	private void initData(){
		RequestParams params = new RequestParams();
		params.put("ACTIVITIES_ID", getIntent().getStringExtra("activityId"));
		HttpRestClient.doHttpGetActivitiesDesc(params, new JsonsfHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode,
					com.alibaba.fastjson.JSONObject response) {
				super.onSuccess(statusCode, response);
				if(response.containsKey("ACTIVITIES_DESC")){
					contentTextV.setText(response.getString("ACTIVITIES_DESC"));
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		}
	}
	
}
